package cn.melon.view.admin.dorm;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.melon.mod.dorm.DormInfo;
import cn.melon.mod.dorm.DormRequest;
import cn.melon.view.admin.dorm.ctr.DormPanelCtr;
import cn.melon.view.admin.dorm.ctr.DormTableMouse;

public class DormJPanel extends JPanel implements IUpdateDormTable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    /*
     * 监听器
     */
    DormPanelCtr listener = new DormPanelCtr(this);
    DormTableMouse mouselistener = new DormTableMouse(this);
    
    /*
     * 查询输入框
     */
    private JTextField jFieldDormId = new JTextField(10);
    private JTextField jFieldStudentId = new JTextField(10);
    
    /*
     * 按钮
     */
    private JButton queryButton = new JButton("查询");
    private JButton addButton= new JButton("添加");
    
    /*
     * 表头
     */
    private String[] tableColName = {"宿舍号","学生学号","宿舍荣誉","宿舍分数","水电费"};
    private JTable table;
    DefaultTableModel tableModel;
    private List<DormInfo> infoList;    // 保存当前的宿舍列表信息
    private DormRequest request = new DormRequest(this);
    
    public DormJPanel() {
        initialize();
        request.getAllDormInfo();
    }
    
    private void initialize() {
        this.setSize(1200, 1000);
        this.setBackground(Color.black);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel topPanel = new JPanel();
        topPanel.setSize(1200, 50);
        topPanel.setBackground(Color.darkGray);

        JPanel dormId = new JPanel();
        JLabel dormIdText = new JLabel("宿舍号:"); 
        dormId.add(dormIdText);
        dormId.add(jFieldDormId);

        JPanel dormName = new JPanel();
        JLabel nameText = new JLabel("学生学号:");
        dormName.add(nameText);
        dormName.add(jFieldStudentId);

        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        topPanel.add(dormId);
        topPanel.add(dormName);
        topPanel.add(queryButton);
        topPanel.add(Box.createRigidArea(new Dimension(400, 15)));
        topPanel.add(addButton);
        this.add(topPanel);

        JScrollPane tablePanel = new JScrollPane();
        tablePanel.setPreferredSize(new Dimension(360, 650));
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel) {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for(int i=0;i<tableColName.length;i++){
            tableModel.addColumn(tableColName[i]);
        }
        tablePanel.setViewportView(table);
        this.add(tablePanel);
        
        /*
         * 添加监听事件
         */
        queryButton.addActionListener(listener);
        addButton.addActionListener(listener);
        table.addMouseListener(mouselistener);
    }
    
    public DormInfo getDormInfo(){
        int row = table.getSelectedRows()[0];
        return infoList.get(row);
    }

    @Override
    public void updateTable(List<DormInfo> dormList) {
        this.infoList = dormList;
        /*
         * 删除全部行
         */
        int rows = tableModel.getRowCount();
        for (int i = 0; i < rows; i++) {
            tableModel.removeRow(0);
        }
        
        /*
         * 添加信息
         */
        for(DormInfo info : dormList){
            String[] dorm = {info.getDormNum(), info.getStudentNum(), 
                            info.getNewHonor(), info.getNewScore(), 
                            info.getWaterAndEletricityCharge()};

            tableModel.addRow(dorm);
        }
    }
}
